<?php

		class Userdata extends CI_Controller {
		
				function __construct(){
						parent::__construct();
				}
				
				function index(){
						die('index');
						
				}
				
				function ajax_pull(){
						$langs = array('en','fr');
						
						if( $this->input->is_ajax_request() && $this->input->post('lang') && $this->input->post('record_type') ) {
								if(in_array($this->input->post('lang'),$langs)) $lang = $this->security->xss_clean($this->input->post('lang'));
								else 											$lang = 'en';
								
								$default = $this->input->post('default_val');
								
								if($this->input->post('record_type')=='title') {
										$titles = new User_title();
										$titles->where('lang',$lang)->order_by('ordering','ASC')->get();
										$items = array();
										foreach($titles as $title){
												$items[] = array('id'=>$title->id, 'name'=>$title->title, 'checked'=>$default==$title->id?'yes':'no');
										}
										die(json_encode( array('stat'=>'success','items'=>$items) ));
								}
								elseif($this->input->post('record_type')=='country') {
										$top_countries = new Country();
										$top_countries->where('lang',$langs[0])->where('list_group',1)->order_by('list_group_sorting','ASC')->get();
										$items = array();
										foreach($top_countries as $country){
												$rel_country = new Country();
												if($lang!=$langs[0])
												{
														$rel_country->where('lang',$lang)->where('iso_num',$country->iso_num)->get(1);
														$items[] = array('id'=>$rel_country->id, 'name'=>$rel_country->name_full, 'checked'=>$default==$rel_country->id?'yes':'no');
												}
												else
												{
														$items[] = array('id'=>$country->id, 'name'=>$country->name_full, 'checked'=>$default==$country->id?'yes':'no');
												}
										}
										$items[] = array('id'=>'','name'=>'----------------');
										
										$countries = new Country();
										$countries->where('lang',$lang)->get();
										foreach($countries as $country){
												$items[] = array('id'=>$country->id, 'name'=>$country->name_full, 'checked'=>$default==$country->id?'yes':'no');
										}
										die(json_encode( array('stat'=>'success','items'=>$items) ));
								}
								die('done');
						}
				}
				
				function _get_iso_countries($lang='en',$no_dup_check=0){
						$data = file_get_contents('http://'.$lang.'.wikipedia.org/wiki/ISO_3166-1');
						$data = str_replace(array("\r\n","\r","\n"),'',$data);						
						$pieces = explode('class="wikitable sortable">',$data);
						$pieces = explode('</table>',$pieces[1]);
						if($pieces[0])
						{
								preg_match_all('|<tr>(.*)</tr>|U',$pieces[0],$items);
								echo '<ol>';
								$country = new Country();
								for($i=1;$i<count($items[1]);$i++)
								{
										preg_match_all('|<td(.*)</td>|U',$items[1][$i],$cols);
										/*
										if($lang!='en') 
										{
											print_r($cols[1]);
											die();
										}
										*/
										$name = '';
										$num = '';
										$a2 = '';
										$a3 = '';
										$num = '';
										$local_name = '';
										if($lang=='fr' && (count($cols[1])==7||count($cols[1])==6))
										{
												preg_match_all('|<tt>(.*)</tt>|U',$cols[1][0],$num);
												$num = isset($num[1][0]) && $num[1][0] ? $this->_htmlentities2(utf8_decode($num[1][0])) : '';
												preg_match_all('|<a href="(.*)" title="(.*)">(.*)</a>|U',$cols[1][4],$name);
												$name = isset($name[3][1]) && $name[3][1] ? $this->_htmlentities2(utf8_decode($name[3][1])) : '';
												preg_match_all('|title="(.*)"|U',$cols[1][2],$a2);
												$a2 = isset($a2[1][0]) && $a2[1][0] ? $this->_htmlentities2(utf8_decode($a2[1][0])) : '';
												preg_match_all('|title="(.*)"|U',$cols[1][1],$a3);
												$a3 = isset($a3[1][0]) && $a3[1][0] ? $this->_htmlentities2(utf8_decode($a3[1][0])) : '';
												$local_name = isset($cols[1][6]) && $cols[1][6] ? str_replace('&gt;','',$this->_htmlentities2(utf8_decode($cols[1][6]))) : '';
										}
										elseif($lang=='en' && count($cols[1])==5)
										{
												preg_match_all('|<a href="(.*)" title="(.*)">(.*)</a>|U',$cols[1][0],$name);
												$name = isset($name[3][0]) && $name[3][0] ? $this->_htmlentities2(utf8_decode($name[3][0])) : '';
												preg_match_all('|<tt>(.*)</tt>|U',$cols[1][1],$a2);
												$a2 = isset($a2[1][0]) && $a2[1][0] ? $this->_htmlentities2(utf8_decode($a2[1][0])) : '';
												preg_match_all('|<tt>(.*)</tt>|U',$cols[1][2],$a3);
												$a3 = isset($a3[1][0]) && $a3[1][0] ? $this->_htmlentities2(utf8_decode($a3[1][0])) : '';
												preg_match_all('|<tt>(.*)</tt>|U',$cols[1][3],$num);
												$num = isset($num[1][0]) && $num[1][0] ? $this->_htmlentities2(utf8_decode($num[1][0])) : '';
										}
										
										echo "<li>$name";
										/*
										if($lang=='fr')
										{
												if(count($cols[1])!=7 && count($cols[1])!=6)  echo " [incorrect count=" . count($cols[1])."] ";
												if(!$name)
												{
														echo " [missing name] ";
												}
												echo " A2=$a2 A3=$a3 NUM=$num Local_name=$local_name</li>";
												continue;
										}
										*/

										//continue;
										//die($name);
										
										$country->clear();
										$country->where('lang',$lang)->where('name_short',trim($this->security->xss_clean($name)))->get(1);
										if($no_dup_check==0 && $country->exists())
										{
												echo " <font color='red'><strong>OK</strong></font></li>\n";
												continue;
										}
										$country->clear();
										
										$country->clear();
										$country->lang 					= $lang;
										$country->rel_id 				= $lang!='en'?$i:0;
										$country->name_short 			= trim($this->security->xss_clean($name));
										$country->name_full 			= trim($this->security->xss_clean($name));
										$country->iso_a2 				= trim($this->security->xss_clean($a2));
										$country->iso_a3				= trim($this->security->xss_clean($a3));
										$country->iso_num				= trim($this->security->xss_clean($num));
										$country->iso_local_name 		= @$local_name;
										$country->tld 					= '';
										$country->currency 				= '';
										$country->calling_code 			= '';
										$country->dialing_prefix 		= '';
										$country->list_sorting 			= 0;
										$country->list_group 			= 0;
										$country->list_group_sorting 	= 0;
										$country->added					= time();
										$country->last_update			= time();
										$country->c_stat 				= 'active';
										$country->save();
										echo " <font color='blue'><strong>OK</strong></font> [#".$this->db->insert_id()."]</li>\n";
										
										//print_r($cols);
										
										//die();
								}
								echo '</ol>';
						}
						
				}
				
				function get_fr_ctry(){
						$data = file_get_contents('http://fxtop.com/fr/showpays.php');
						preg_match_all('|<TR><TD>(.*)</TD><TD>|U',str_replace("\n",'',$data),$items);
						for($i=1;$i<count($items[1]);$i++)
						{
								$country = new Country();
								$country->lang = 'fr';
								$country->country = $this->_htmlentities2($this->security->xss_clean($items[1][$i]));
								$country->save();
						}
						die('done');
				}
				
				/**
				* Convert entities, while preserving already-encoded entities.
				*
				* @link http://www.php.net/htmlentities Borrowed from the PHP Manual user notes.
				*
				* @since 1.2.2
				*
				* @param string $myHTML The text to be converted.
				* @return string Converted text.
				*/
				function _htmlentities2($myHTML) {
					$translation_table = get_html_translation_table( HTML_ENTITIES, ENT_QUOTES );
					$translation_table[chr(38)] = '&';
					$translation_table[chr(130)] = '&sbquo;';    // Single Low-9 Quotation Mark
					$translation_table[chr(131)] = '&fnof;';    // Latin Small Letter F With Hook
					$translation_table[chr(132)] = '&bdquo;';    // Double Low-9 Quotation Mark
					$translation_table[chr(133)] = '&hellip;';    // Horizontal Ellipsis
					$translation_table[chr(134)] = '&dagger;';    // Dagger
					$translation_table[chr(135)] = '&Dagger;';    // Double Dagger
					$translation_table[chr(136)] = '&circ;';    // Modifier Letter Circumflex Accent
					$translation_table[chr(137)] = '&permil;';    // Per Mille Sign
					$translation_table[chr(138)] = '&Scaron;';    // Latin Capital Letter S With Caron
					$translation_table[chr(139)] = '&lsaquo;';    // Single Left-Pointing Angle Quotation Mark
					$translation_table[chr(140)] = '&OElig;    ';    // Latin Capital Ligature OE
					$translation_table[chr(145)] = '&lsquo;';    // Left Single Quotation Mark
					$translation_table[chr(146)] = '&rsquo;';    // Right Single Quotation Mark
					$translation_table[chr(147)] = '&ldquo;';    // Left Double Quotation Mark
					$translation_table[chr(148)] = '&rdquo;';    // Right Double Quotation Mark
					$translation_table[chr(149)] = '&bull;';    // Bullet
					$translation_table[chr(150)] = '&ndash;';    // En Dash
					$translation_table[chr(151)] = '&mdash;';    // Em Dash
					$translation_table[chr(152)] = '&tilde;';    // Small Tilde
					$translation_table[chr(153)] = '&trade;';    // Trade Mark Sign
					$translation_table[chr(154)] = '&scaron;';    // Latin Small Letter S With Caron
					$translation_table[chr(155)] = '&rsaquo;';    // Single Right-Pointing Angle Quotation Mark
					$translation_table[chr(156)] = '&oelig;';    // Latin Small Ligature OE
					$translation_table[chr(159)] = '&Yuml;';    // Latin Capital Letter Y With Diaeresis
					return preg_replace( "/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/", "&amp;", strtr($myHTML, $translation_table) );
				}
		
		}

/* End of file users.php */
/* Location: ./system/application/controller/user.php */